package com.cstp.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cstp.entity.Prize;
import com.cstp.entity.PrizeHis;
import com.cstp.manage.PrizeManage;
import com.cstp.web.req.PageReq;
import com.cstp.web.req.PrizeLogReq;
import com.cstp.web.req.PrizeLotteryReq;
import com.cstp.web.res.PrizeResp;
import com.cstp.web.res.Resp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author watts
 */
@RestController
public class PrizeController {

    @Resource
    private PrizeManage prizeManage;

    @PostMapping("/prize/detail/{id}")
    public Resp<Prize> detail(@PathVariable Long id) {
        return Resp.ok(new Prize().selectById(id));
    }

    @PostMapping("/prize/update")
    public Resp<Boolean> update(@RequestBody Prize param) {
        return Resp.ok(param.updateById());
    }

    @PostMapping("/prize/list")
    public Resp<IPage<Prize>> list(@RequestBody PageReq param) {
        IPage<Prize> page = new Page<>(param.getPageNum(), param.getPageSize());
        Prize prize = new Prize();
        page = prize.selectPage(page, new QueryWrapper<>(prize));
        return Resp.ok(page);
    }

    @PostMapping("/prize/his/list")
    public Resp<IPage<PrizeHis>> hisList(@RequestBody PrizeLogReq param) {
        IPage<PrizeHis> page = new Page<>(param.getPageNum(), param.getPageSize());
        PrizeHis prizeHis = new PrizeHis();
        page = prizeHis.selectPage(page, new QueryWrapper<>(prizeHis)
                .eq(Objects.nonNull(param.getPrizeId()), "prize_id", param.getPrizeId())
                .like(Objects.nonNull(param.getPhone()), "phone", param.getPhone())
                .eq(Objects.nonNull(param.getState()), "state", param.getState())
                .orderByDesc("create_time"));
        return Resp.ok(page);
    }

    @PostMapping("/prize/his/update/{id}/{state}")
    public Resp<Boolean> exchangePrize(@PathVariable Long id,
                                       @PathVariable Integer state) {
        PrizeHis his = new PrizeHis();
        his.setId(id);
        his.setState(state);
        return Resp.ok(his.updateById());
    }

    @PostMapping("/api/prize/list")
    public Resp<List<PrizeResp>> list(@RequestHeader("token") String toKen) {
        Prize prize = new Prize();
        return Resp.ok(prize.selectAll()
                            .stream()
                            .map(PrizeResp::of)
                            .collect(Collectors.toList())
        );
    }

    @PostMapping("/api/prize/lottery")
    public Resp<Boolean> lottery(@RequestHeader("token") String toKen,
                                 @RequestBody PrizeLotteryReq param) {
        return Resp.ok(prizeManage.lottery(param, toKen));
    }

    @PostMapping("/api/prize/pre/check/{id}")
    public Resp<Boolean> preCheck(@RequestHeader("token") String toKen,
                                  @PathVariable Long id) {
        return Resp.ok(prizeManage.preCheck(id, toKen));
    }

}
